<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getMenus">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

    <!--列表-->
    <el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="菜单名称" width="100">
      </el-table-column>
      <el-table-column prop="url" label="菜单路由" width="100">
      </el-table-column>
      <el-table-column prop="icon" label="图标" width="100">
      </el-table-column>
      <el-table-column prop="parent.name" label="父菜单" min-width="100">
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="block">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="query.currentPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageInfo.total"
          style="float:right;">
      </el-pagination>
    </el-col>


    <el-dialog title="编辑页面" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="名称" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单路由" prop="url">
          <el-input v-model="saveForm.url" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-input v-model="saveForm.icon" auto-complete="off"></el-input>
        </el-form-item>
<!--        <el-form-item label="上级菜单">-->
<!--          <el-cascader v-model="saveForm.parent"-->
<!--                       :options="menuTree"-->
<!--                       :props="{-->
<!--                 emitPath:false,-->
<!--							   checkStrictly: true,-->
<!--							   label:'name',-->
<!--							   value:'id',-->
<!--							   children: 'children'-->
<!--							}"-->
<!--                       clearable></el-cascader>-->
<!--        </el-form-item>-->


        <el-form-item label="上级菜单" prop="saveForm.parent">
          <el-select v-model="saveForm.parent" clearable value-key="id" placeholder="请选择">
            <!--下拉选项
              :key=""  //唯一标识
              :label 选择之后展示到选择框中的值
              :value 选中之后绑定给模型层的值  如果要绑定对象给模型层 有一个大坑
                 必须要写  value-key="id"
            -->
            <el-option
                v-for="item in menus"
                :key="item.id"
                :label="item.name"
                :value="item">
              <span style="float: left">{{ item.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>



      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>
	</section>
</template>

<script>
	export default {
		data() {
			return {
				filters: {
					name: ''
				},
        pageInfo: {
				  rows:[],
          total:0
        },
        query: {
          currentPage: 1,
          pageSize: 10,
          keyword:""
        },
        menuTree: [],
        menus: [],
				total: 0,
				page: 1,
				listLoading: false,
				sels: [],//列表选中列


				saveLoading: false,
				//编辑界面数据


				saveFormVisible: false,//新增界面是否显示
				saveFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//新增界面数据
				saveForm: {
					name: '',
					url: '',
          icon: '',
          parent: {
					  id:null,
            name:''
          }
				}

			}
		},
		methods: {
      getMenus1(){
        this.$http.get("/menu").then(res=>{
          if(res.data.success){
            this.menus=res.data.obj;
          }
        })
      },
			handleCurrentChange(val) {
				this.query.currentPage = val;
				this.getMenus();
			},
      handleSizeChange(val) {
        this.query.pageSize=val;
        this.getMenus();
      },
			getMenus() {
				this.listLoading = true;

				this.$http.post("/menu",this.query).then(result=>{
          this.listLoading = false;
          if(result.data.success){
            this.pageInfo.rows=result.data.obj.pageList;

            this.pageInfo.total=result.data.obj.total;
          }else {
            this.$message({
              message: '查询失败',
              type: 'info'
            });
          }
        })
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
          console.log(row.id);
          this.$http.delete("/menu/"+row.id).then((res) => {
						this.listLoading = false;
						if(res.data.success){
              this.$message({
                message: '删除成功',
                type: 'success'
              });
              this.query.currentPage=1;
              this.getMenus();
            }else {
              this.$message({
                message: '删除失败',
                type: 'info'
              });
            }
						//NProgress.done()
					});
				}).catch(() => {
          this.$message({
            message: '网络出错了',
            type: 'info'
          });
				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.saveFormVisible = true;
        this.getMenus1();
        this.getTree();
        this.saveForm = row

			},
      getTree(){
			  this.$http.get("/menu/tree").then(res=>{

          if(res.data.success){
			      this.menuTree=res.data.obj;

          }
        })
      },
			//显示新增界面
			handleAdd: function () {
        console.log("xinzen")
				this.saveFormVisible = true;
        this.getTree();
        this.getMenus1();
				this.saveForm = {
          name: '',
          url: '',
          icon: '',
          parent: {
            id:null,
            name:''
          }
        }
			},

			//新增
      saveSubmit: function () {
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.$http.put("/menu",this.saveForm).then((res) => {
                if(res.data.success) {
                  this.$message({
                    message: '提交成功',
                    type: 'success'
                  });
                  this.saveFormVisible = false;
                  this.query.currentPage=1;
                  this.getMenus();
                }
							});
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
          this.listLoading = true;
          console.log(ids);
          this.$http.patch("/menu",ids).then((res) => {
            this.listLoading = false;
            if(res.data.success){
              this.$message({
                message: '删除成功',
                type: 'success'
              });
              this.query.currentPage=1;
              this.getMenus();
            }else {
              this.listLoading = false;
              this.$message({
                message: '删除失败',
                type: 'info'
              });
            }
            //NProgress.done()
          });
				}).catch(() => {
          this.listLoading = false;
				});
			}
		},
		mounted() {
			this.getMenus();
		}
	}

</script>

<style scoped>

</style>